/**
 * @name path
 * @author Sowmya Balasubramanian, Aaron Hong, David Robertson
 */

/*
 * This is basically only path now... it still needs to be overhauled because
 * it calls GPolyline stuff in here.
 */

function PathParams() {
    this.pathsData = undefined;
    this.pathToRemove = undefined;
    this.pathToAdd = undefined;
    this.pathOnMap = undefined;
    this.pathName = undefined;
    this.pathNameCoord = undefined;
}

function getPaths(){
    console.log("getPaths called");
    GDownloadUrl(servletUrl + "servlet/?action=getPaths", function(data){
        if (checkError(data)) {
            return;
        }
        if (typeof(JSON) === 'object' &&
            typeof(JSON.parse) === 'function') {
            wm.path.pathsData = JSON.parse(data); // native parsing available
        } else {
            wm.path.pathsData = eval("(" + data + ")");
        }
        //console.log(wm.path.pathsData);
        GEvent.trigger(wm.weathermap.eventDummy, "pathsChanged");
    })
}


function makePath(){
    console.log("makePath.start");
    if (wm.weathermap.pathChoice == "none" || wm.weathermap.pathChoice == "undef") {
        return;
    }

    //console.log(wm.path.pathsData);
    var tmpPath;
    var circuit = wm.path.pathsData[wm.weathermap.pathChoice]["hops"]["lod_" + wm.weathermap.lod];
    var numberOfHops = circuit.length;
    var index = 1;
    var type = "SDN"; //I don't think this really matters
    var pathCurve = new Array(numberOfHops);
    // lengthy kludge for SC09 to draw link to Portland
    var insertFirst = "";
    var insertLast = "";
    if (wm.weathermap.pathChoice.startsWith("sc09")) {
        if (wm.weathermap.lod == 0) {
            if (circuit[0] == "PNWG") {
                insertFirst = "PNWG";
            } else if (circuit[numberOfHops-1] == "PNWG") {
                insertLast = "PNWG";
            } else if (circuit[0] == "SUNN") {
                insertFirst = "SUNN";
            } else if (circuit[numberOfHops-1] == "SUNN") {
                insertLast = "SUNN";
            }
        } else {
            if (circuit[0] == "pnwg-sdn1") {
                insertFirst = "pnwg-sdn1";
            } else if (circuit[numberOfHops-1] == "pnwg-sdn1") {
                insertLast = "pnwg-sdn1";
            } else if (circuit[0] == "sunn-sdn2") {
                insertFirst = "sunn-sdn2";
            } else if (circuit[numberOfHops-1] == "sunn-sdn2") {
                insertLast = "sunn-sdn2";
            }
        }
    }
    var arraySize = numberOfHops;
    if (insertFirst || insertLast) {
        arraySize++;
    }
    var nodeLat=new Array(arraySize);
    var nodeLong=new Array(arraySize);
    console.log("Num of hops=",numberOfHops);
    var sc09node = wm.topology.topologyData[type][wm.weathermap.lod]["nodes"]["SC09"];
    console.log("insertFirst: " + insertFirst);
    console.log("insertLast: " + insertLast);
    var zoom;
    var adjPoint;
    // horrible
    if (wm.weathermap.flash) {
        zoom = getFlexApp('weathermap').getZoom();
        if (zoom < 4) {
            adjPoint = new GLatLng(sc09node.latitude - 3.0, sc09node.longitude + 10.0);
        } else if (zoom == 4) {
            adjPoint = new GLatLng(sc09node.latitude - 1.5,
                                   sc09node.longitude + 1.0);
        } else {
            adjPoint = new GLatLng(sc09node.latitude, sc09node.longitude);
        }
    } else {
        zoom = wm.renderer.map.getZoom();
        if (zoom < 4) {
            adjPoint = new GLatLng(sc09node.latitude - 3.0, sc09node.longitude + 10.0);
        // Firefox behavior different
        } else if (window.globalStorage && window.postMessage && (zoom < 9)) {
            adjPoint = new GLatLng(sc09node.latitude - 1.5,
                                   sc09node.longitude + 1.0);
        } else if (zoom == 4) {
            adjPoint = new GLatLng(sc09node.latitude - 1.5,
                                   sc09node.longitude + 1.0);
        } else {
            adjPoint = new GLatLng(sc09node.latitude, sc09node.longitude);
        }
    }
    if (insertFirst) {
        nodeLat[0] = adjPoint.lat();
        nodeLong[0] = adjPoint.lng();
    }
    for (var i = 0; i < numberOfHops; i++) {
        var node = wm.topology.topologyData[type][wm.weathermap.lod]["nodes"][circuit[i]];
        if (!insertFirst) {
            nodeLat[i] = node.latitude;
            nodeLong[i] = node.longitude;
        } else {
            nodeLat[i+1] = node.latitude;
            nodeLong[i+1] = node.longitude;
        }

        if ((numberOfHops == 1) && !insertFirst && !insertLast) {
            var nodeLat=new Array(numberOfHops+3);
            var nodeLong=new Array(numberOfHops+3);
            nodeLat[0] = node.latitude;
            nodeLong[0] = node.longitude;
            nodeLat[1] = node.latitude-1;
            nodeLong[1]=node.longitude+1;
            nodeLat[2] = node.latitude-1;
            nodeLong[2]=node.longitude-1;
            nodeLat[3] = node.latitude;
            nodeLong[3] = node.longitude;
        }
    }
    if (insertLast) {
        nodeLat[numberOfHops] = adjPoint.lat();
        nodeLong[numberOfHops] = adjPoint.lng;
    }
    var pathname=wm.path.pathsData[wm.weathermap.pathChoice]["description"];
    if (insertFirst || insertLast) {
        numberOfHops++;
    }
    createPathDS(nodeLat,nodeLong,numberOfHops,
                wm.path.pathsData[wm.weathermap.pathChoice].bandwidth,pathname);
}
